package com.mccraywebtech.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mccraywebtech.models.Contact;
import com.mccraywebtech.models.EmailUser;
import com.mccraywebtech.services.GenericService;
import com.mccraywebtech.services.email.impl.EmailServiceImpl;

@Controller("emailController")
public class EmailController {
	@Autowired
	@Qualifier("emailService")
	private EmailServiceImpl emailService;
	@Qualifier("contactService")
	private GenericService<Contact> contactService;
	
	/**
	 * @return the emailService
	 */
	public EmailServiceImpl getEmailService() {
		return emailService;
	}

	/**
	 * @param emailService the emailService to set
	 */
	public void setEmailService(EmailServiceImpl emailService) {
		this.emailService = emailService;
	}

	/**
	 * @return the contactService
	 */
	public GenericService<Contact> getContactService() {
		return contactService;
	}

	/**
	 * @param contactService the contactService to set
	 */
	public void setContactService(GenericService<Contact> contactService) {
		this.contactService = contactService;
	}

	@RequestMapping("/email/{contactId}")
	public String emailContact(@PathVariable("contactId") Integer contactId,
			Map<String, Object> map) {
		Contact c = (Contact) contactService.getObject(contactId);
		
		map.put("emailUser", new EmailUser());
		map.put("contact", c);

		return "email";
	}
	
	@RequestMapping(value = "/email/sendEmail", method = RequestMethod.POST)
	public String sendEmail(@ModelAttribute("emailUser") EmailUser emailUser, BindingResult result) {
		emailService.sendEmail(emailUser);
		return "redirect:/index";
	}
}
